package artibella.media.controls.views
{
	import artibella.media.audio.AudioMedia;
	import artibella.media.controls.AudioController;
	import artibella.media.controls.ui.InfoButton;
	import artibella.media.controls.ui.ListButton;
	import artibella.media.controls.ui.PlayPauseButton;
	import artibella.media.controls.ui.PrevNextButton;
	import artibella.media.controls.ui.ProgressionTool;
	import artibella.media.controls.ui.StopButton;
	import artibella.media.controls.ui.TimeTool;
	import artibella.media.controls.ui.VolumeSlider;
	import artibella.media.events.MediaEvent;
	import artibella.StageManager;
	import artibella.text.FontsManager;
	import artibella.text.CrawlingLabel;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	
	
	/**
	 * ...
	 * @author pbordachar
	 */
	
	public class RTB_AudioController extends AudioController
	{
		
		private var _playlist:RTB_PlayList;
		private var _spectrum:RTB_Spectrum;
		private var _copyright:TextField;
		
		public function RTB_AudioController() 
		{
			super();
		}
		
		// - - -
		
		override public function init():void
		{
			//_drawSeparator( -20 );
			
			_drawBasicMediaControls();
			
			_drawSeparator( 20 );
			
			_drawExtendedControls();
			
			_drawSeparator( 50 );
			
			// - - - 

			_showList( null );
			
			_showSpectrum( null );
			
		}
		
		private function _drawBasicMediaControls():void
		{
			setPlayBt( new PlayPauseButton( 0x333333 ), 10, 0 );
			setStopBt( new StopButton( 0x333333 ), 35, 0 );
			setProgressionTool( new ProgressionTool( this, 100, 0x333333 ), 60, 0 );
			setTimeTool( new TimeTool( TimeTool.TIME ), 170, 0 );
			setVolumeTool( new VolumeSlider( this, 0x333333 ), 220, 1 );
		}
		
		private function _drawSeparator( py:int ):void
		{
			var s:Sprite = new Sprite();
			s.graphics.lineStyle( 1, 0x000000, .3 );
			s.graphics.moveTo( 2, py );
			s.graphics.lineTo( 228, py );
			//s.blendMode = BlendMode.OVERLAY;
			addChild( s );
		}
		
		private function _drawExtendedControls():void
		{
			setPrevBt( new PrevNextButton( 0x333333 ), 5, 35 );
			setNextBt( new PrevNextButton( 0x333333 ), 20, 35 );
			setLabel( new CrawlingLabel( 145, true ), 35, 35 );
			setInfoBt( new InfoButton( 0x333333 ), 200, 35 );
			setListBt( new ListButton( 0x333333 ), 220, 35 );
			
			_bt_prev.scaleX = -1;
		}
		
		private function _addCopyrightText():void
		{
			var format:TextFormat = new TextFormat();
			format.font = FontsManager.getFontByName( "Arial Narrow" );
			format.size = 7.4;
			format.align = TextFormatAlign.JUSTIFY;
			format.leading = 2;
			format.letterSpacing = .2;
			format.color = 0x000000;

			_copyright = new TextField();
			_copyright.antiAliasType = AntiAliasType.ADVANCED;
			_copyright.embedFonts = true;
			_copyright.autoSize = "left";
			_copyright.wordWrap = true;
			_copyright.multiline = true;
			_copyright.selectable = false;
			_copyright.width = 230;
			_copyright.text = "ALL RIGHTS RESERVED TO THEIR PARTICULAR OWNERS. I DON'T OWN ANY COPYRIGHTS FOR THIS SONGS. IF YOU LIKE THE MUSIC, PLEASE SUPPORT THE ARTISTS BY BUYING IT IN YOUR FAVORITE MUSIC STORE.";
			_copyright.setTextFormat( format );
			_copyright.x = -3;
			_copyright.y = 61; // 120;
			_copyright.alpha = .5
			addChild( _copyright );
		}
		
		private function _removeCopyrightText():void
		{
			removeChild( _copyright );
			_copyright = null;
			
		}
		
		// - - -
		
		override public function set labelText( s:String ):void
		{
			if ( _label != null ) _label.text = s.toUpperCase();
		}
		
		///////  M E D I A   E V E N T S
		
		override protected function _onPlay( e:MediaEvent ):void
		{
			_bt_play.drawPause();
			
			if ( _spectrum != null ) _spectrum.play();
			
			super._onPlay( e );
		}
		
		override protected function _onPause( e:MediaEvent ):void
		{
			_bt_play.drawPlay();
			
			if ( _spectrum != null ) _spectrum.stop();

			super._onPause( e );
		}
		
		override protected function _onStop( e:MediaEvent ):void
		{
			_bt_play.drawPlay();
			
			if ( _spectrum != null ) _spectrum.stop();
			
			super._onStop( e );
		}
		
		/////// P L A Y L I S T
		
		override protected function _showList( e:MouseEvent ):void
		{
			if ( _player.list == null  ) return;
			
			_bt_list.alpha = .7;
			_bt_list.removeEventListener( MouseEvent.CLICK, _showList );
			_bt_list.addEventListener( MouseEvent.CLICK, _hideList );
			
			_buildPlaylist();
		}
		
		private function _buildPlaylist():void
		{
			if ( _playlist != null ) return;
	
			_playlist = new RTB_PlayList( _player.list.items );
			_playlist.addEventListener( MouseEvent.CLICK, _onListItemSelected );
			_playlist.x = 0;
			_playlist.y = 60;
			addChild( _playlist );
			
			_addCopyrightText();
			
			if( _spectrum != null ) _spectrum.y = 111;
			if( _copyright != null ) _copyright.y = ( _spectrum != null ) ? 221 : 161;

			StageManager.instance.getStage().dispatchEvent( new Event( Event.RESIZE ) );
		}
		
		private function _onListItemSelected( e:MouseEvent ):void
		{
			trace( e.target );
			
			if ( !(e.target is CrawlingLabel ) ) return;
			
			_player.play( e.target.indice );
		}
		
		override protected function _hideList( e:MouseEvent ):void
		{
			_bt_list.alpha = 1;
			_bt_list.removeEventListener( MouseEvent.CLICK, _hideList );
			_bt_list.addEventListener( MouseEvent.CLICK, _showList );
			
			if ( _playlist == null ) return;
			
			removeChild( _playlist );
			
			_removeCopyrightText();
			
			_playlist.removeEventListener( MouseEvent.CLICK, _onListItemSelected );
			_playlist.dispose();
			_playlist = null;
			
			if ( _spectrum != null ) _spectrum.y = 11;
			if ( _copyright != null ) _copyright.y = ( _spectrum != null ) ? 121 : 61;
			
			StageManager.instance.getStage().dispatchEvent( new Event( Event.RESIZE ) );
		}
		
		/////// S P E C T R U M
		
		override protected function _showInfos( e:MouseEvent ):void
		{
			_bt_info.removeEventListener( MouseEvent.CLICK, _showInfos );
			
			_showSpectrum( null );
		}
		
		protected function _showSpectrum( e:MouseEvent ):void
		{
			if ( _spectrum != null ) return;
			
			_bt_info.alpha = .7;
			_bt_info.removeEventListener( MouseEvent.CLICK, _showInfos ); 

			_bt_info.removeEventListener( MouseEvent.CLICK, _showSpectrum );
			_bt_info.addEventListener( MouseEvent.CLICK, _hideSpectrum );
			
			_buildSpectrum();
			
			StageManager.instance.getStage().dispatchEvent( new Event( Event.RESIZE ) );
		}
		
		private function _buildSpectrum():void
		{
			_spectrum = new RTB_Spectrum( 0x333333 );
			_spectrum.x = 0;
			_spectrum.y = ( _playlist != null ) ? 111 : 11 ;
			addChildAt( _spectrum, 0 );
			
			if ( _spectrum != null ) _spectrum.play();
			if ( _copyright != null ) _copyright.y = _spectrum.y + 110; // 221;
		}
		
		protected function _hideSpectrum( e:MouseEvent ):void
		{
			if ( _spectrum == null ) return;
			
			_bt_info.alpha = 1;
			_bt_info.addEventListener( MouseEvent.CLICK, _showSpectrum );
			_bt_info.removeEventListener( MouseEvent.CLICK, _hideSpectrum );
			
			removeChild( _spectrum );
			
			_spectrum.dispose();
			_spectrum = null;
			
			if ( _copyright != null ) _copyright.y = ( _playlist != null ) ? 161 : 61;
			
			StageManager.instance.getStage().dispatchEvent( new Event( Event.RESIZE ) );
		}
	}
}